﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace CrazyTalk.Common
{
    /// <summary>
    /// C#类型常用判断法
    /// 1. 正则匹配法
    /// 2. 是否可转换法
    /// 3. 
    /// </summary>
    public class ValidationUtil
    {
        private readonly static DateTime MinDateTime = new DateTime(1900, 1, 1);
        #region 常用的正则表达式定义

        // 所有电话        
        private static Regex regAllPhone = new Regex("(((86)|(086)|(86-)|(086-))?(((0)?13\\d{9})|((\\(\\d{3,4}\\)|\\d{3,4}-)?\\d{7,8})))((-)?\\d{0,4})");
        //移动电话
        //^(13|15)[0-9]{9}$
        //((\\(\\d{3}\\))|(\\d{3}\\-))?13\\d{9}|15[89]\\d{8}
        private static Regex regMobile = new Regex("(13|15|18)[0-9]{9}");
        //固定电话
        //^[0-9]+[-]?[0-9]+[-]?[0-9]$
        private static Regex regPhone = new Regex("(([0\\+]\\d{2,3}-)?(0\\d{2,3})-)?(\\d{7,8})(-(\\d{3,}))?");//0\\d{2,3}-\\d{7,8}
        private static Regex regNumber = new Regex("[0-9]+");
        private static Regex regNumberSign = new Regex("[+-]?[0-9]+");
        private static Regex regInt = new Regex("[0-9]*[1-9][0-9]*");
        private static Regex regDecimal = new Regex("([0-9]+)|([0-9]+[.]?[0-9]+)");
        private static Regex regDecimalSign = new Regex("[+-]?[0-9]+[.]?[0-9]+"); //等价于^[+-]?\d+[.]?\d+$
        private static Regex regEmail = new Regex("[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)");//w 英文字母或数字的字符串，和 [a-zA-Z0-9] 语法一样 
        private static Regex regCHZN = new Regex("[\u4e00-\u9fa5]");
        private static Regex regNumEnglish = new Regex("[0-9]|[a-zA-Z]");
        private static Regex regIP = new Regex("(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])((\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])){3}|(\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])){5})");

        #endregion
        #region 正则判断方式

        #region 数字字符串检查
        public static bool IsPhone(string inputData)
        {
            Match m = regPhone.Match(inputData);
            return m.Success;
        }
        /// <summary>
        /// 是否为移动号码
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsMobile(string input)
        {
            return regMobile.Match(input).Success;
        }
        /// <summary>
        /// 判断是否正整数
        /// </summary>
        /// <param name="strNumber"></param>
        /// <returns></returns>
        public static bool IsInt(String strNumber)
        {
            return regInt.IsMatch(strNumber);
        }
        /// <summary>
        /// 是否数字字符串
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsNumber(string inputData)
        {
            Match m = regNumber.Match(inputData);
            return m.Success;
        }

        /// <summary>
        /// 是否数字字符串 可带正负号
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsNumberSign(string inputData)
        {
            Match m = regNumberSign.Match(inputData);
            return m.Success;
        }
        /// <summary>
        /// 是否是浮点数
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsDecimal(string inputData)
        {
            Match m = regDecimal.Match(inputData);
            return m.Success;
        }
        /// <summary>
        /// 是否是浮点数，并且数值在指定范围内，包含边界
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsDecimalRange(string input, decimal min, decimal max)
        {
            Match m = regDecimal.Match(input);
            if (!m.Success)
                return false;
            decimal d = decimal.Parse(input);

            return d >= min && d <= max;
        }
        /// <summary>
        /// 是否是浮点数 可带正负号
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsDecimalSign(string inputData)
        {
            Match m = regDecimalSign.Match(inputData);
            return m.Success;
        }
        #endregion

        #region 中文检测
        /// <summary>
        /// 检测是否有中文字符
        /// </summary>
        /// <param name="inputData"></param>
        /// <returns></returns>
        public static bool IsHasCHZN(string inputData)
        {
            Match m = regCHZN.Match(inputData);
            return m.Success;
        }

        #endregion

        /// <summary>
        /// 检查输入是否为ip地址字符串
        /// </summary>
        /// <param name="input">输入字符串</param>
        /// <returns></returns>
        public static bool IsIPAddress(string input)
        {
            //原作者表达式@"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$"
            return Regex.IsMatch(input, @"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])((\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])){3}|(\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])){5})$");
        }

        /// <summary>
        /// 检查输入是否为ip地址字符串或IP段
        /// </summary>
        /// <param name="input">输入字符串</param>
        /// <returns></returns>
        public static bool IsIPAddressOrSection(string input)
        {
            return Regex.IsMatch(input, @"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])(\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])){2}((\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5]){1,3})|(\.\*))$");
        }

        /// <summary>
        /// 是否是浮点数 可带正负号
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsEmail(string inputData)
        {
            Match m = regEmail.Match(inputData);
            return m.Success;
        }
        #region 身份证号码判断
        /*
        private static Regex regIDCard = new Regex("[1-9]([0-9]{14}|[0-9]{17})");
        /// <summary>
        /// 是否身份证号码格式(简单判断)
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsIDCard(string input)
        {
            return regIDCard.Match(input).Success;
        }
        */

        /// <summary> 验证输入字符串为18位的身份证号码 </summary>
        /// <param name="strln">输入的字符</param>
        /// <returns></returns>
        public static bool IsIDCard18(string strln)
        {
            long n = 0;
            if (long.TryParse(strln.Remove(17), out n) == false || n < Math.Pow(10, 16) || long.TryParse(strln.Replace('x', '0').Replace('X', '0'), out n) == false)
            {
                return false;//数字验证
            }
            string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
            if (address.IndexOf(strln.Remove(2)) == -1)
            {
                return false;//省份验证
            }
            string birth = strln.Substring(6, 8).Insert(6, "-").Insert(4, "-");
            DateTime time = new DateTime();
            if (DateTime.TryParse(birth, out time) == false)
            {
                return false;//生日验证
            }
            string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');
            string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
            char[] Ai = strln.Remove(17).ToCharArray();
            int sum = 0;
            for (int i = 0; i < 17; i++)
            {
                sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());
            }
            int y = -1;
            Math.DivRem(sum, 11, out y);
            if (arrVarifyCode[y] != strln.Substring(17, 1).ToLower())
            {
                return false;//校验码验证
            }
            return true;//符合GB11643-1999标准
        }
        /// <summary> 验证输入字符串为15位的身份证号码 </summary>
        /// <param name="strln">输入的字符</param>
        /// <returns></returns>
        public static bool IsIDCard15(string strln)
        {
            long n = 0;
            if (long.TryParse(strln, out n) == false || n < Math.Pow(10, 14))
            {
                return false;//数字验证
            }
            string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
            if (address.IndexOf(strln.Remove(2)) == -1)
            {
                return false;//省份验证
            }
            string birth = strln.Substring(6, 6).Insert(4, "-").Insert(2, "-");
            DateTime time = new DateTime();
            if (DateTime.TryParse(birth, out time) == false)
            {
                return false;//生日验证
            }
            return true;//符合15位身份证标准
        }
        /// <summary> 验证身份证是否有效</summary>
        /// <param name="strln"></param>
        /// <returns></returns>
        public static bool IsIDCard(string strln)
        {
            if (strln.Length == 18)
            {
                bool check = IsIDCard18(strln);
                return check;
            }
            else if (strln.Length == 15)
            {
                bool check = IsIDCard15(strln);
                return check;
            }
            else
            {
                return false;
            }
        }
        #endregion
        /// <summary>
        /// 输入字符是否为银行卡号（目前仅判断是否为数字及位数为10-21）
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsBankNumber(string input)
        {
            return IsNumber(input) && input.Length > 9 && input.Length < 21;
        }

        #endregion

        #region 日期格式判断
        /// <summary>
        /// 日期格式字符串判断
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsDateTime(string str)
        {
            try
            {
                if (!string.IsNullOrEmpty(str))
                {
                    DateTime dt = DateTime.Parse(str);
                    //如果小于最小时间，则返回假，防止数据库操作溢出错误
                    return (dt > MinDateTime);
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }
        #endregion

        /// <summary>
        /// 验证是否是数字或英文的
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsNumEnglish(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return false;
            }
            Match m = regNumEnglish.Match(str);
            return m.Success;
        }
        public static bool IsIP(string ip)
        {
            Match m = regIP.Match(ip);
            return m.Success;
        }

        #region 通过正则判断输入是否为指定的数据类型
        /// <summary> 检验邮件地址格式是否正确
        /// 
        /// </summary>
        /// <param name="mailAddress">邮件地址
        /// </param>
        public static bool IsValidMailAddress(string emailAddress)
        {
            string pattern = @"^[_a-zA-Z0-9-]+(.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(.[a-zA-Z0-9-]+)*$";
            return Regex.Match(emailAddress, pattern).Success;
        }

        /// <summary> 
        /// 检验手机号码格式是否正确
        /// </summary>        
        public static bool IsValidMobeil(string mobeil)
        {
            string pattern = @"^[1][3][0-9]{9}$";

            return Regex.Match(mobeil, pattern).Success;
        }

        /// <summary> 
        /// 检验邮件地址格式是否正确
        /// </summary>
        /// <param name="mailAddress">邮件地址
        /// </param>
        public static bool IsValidMailAddress(string emailAddress, string str)
        {
            string pattern = @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";

            return Regex.Match(emailAddress, pattern).Success;
        }

        /// <summary>
        /// 验证电话号码，传真号码是否正确(可以允许电话号码为空)限制长度7个字以上
        /// </summary>
        /// <param name="TelNum"></param>
        /// <returns></returns>
        public static bool IsValidTelNum(string TelNum)
        {
            if (TelNum == "")
            {
                return true;
            }
            else
            {
                string pattern = @"(((\(\d{2,3}\))|(\d{2,3}-))\d{7,8})|\d{7,11}";
                return Regex.Match(TelNum, pattern).Success;
            }
        }
        #region 字符判断及格式化

        /// <summary>
        /// 判断是否数字 
        /// </summary>
        /// <param name="strNumber"></param>
        /// <returns></returns>
        public static bool IsNumber2(String strNumber)
        {
            Regex objNotNumberPattern = new Regex("[^0-9.-]");
            Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*");
            Regex objTwoMinusPattern = new Regex("[0-9]*[-][0-9]*[-][0-9]*");
            String strValidRealPattern = "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
            String strValidIntegerPattern = "^([-]|[0-9])[0-9]*$";
            Regex objNumberPattern = new Regex("(" + strValidRealPattern + ")|(" + strValidIntegerPattern + ")");

            return !objNotNumberPattern.IsMatch(strNumber) &&
            !objTwoDotPattern.IsMatch(strNumber) &&
            !objTwoMinusPattern.IsMatch(strNumber) &&
            objNumberPattern.IsMatch(strNumber);
        }
        /// <summary>
        /// 判断是否浮点数
        /// </summary>
        /// <param name="strNumber"></param>
        /// <returns></returns>
        public static bool IsDouble(String strNumber)
        {
            bool b = true;
            try
            {
                double d = Convert.ToDouble(strNumber);
            }
            catch
            {
                b = false;
            }
            return b;
        }

        #endregion 字符判断及格式化
        #endregion
        /// <summary>
        /// 验证电话号码，传真号码是否正确(可以允许电话号码为空),不限制长度
        /// </summary>
        /// <param name="TelNum"></param>
        /// <returns></returns>
        public static bool IsValidTelNumNotLength(string TelNum)
        {
            if (TelNum == "")
            {
                return true;
            }
            else
            {
                //string pattern = @"(((\(\d{2,3}\))|(\d{2,3}-))\d{1,8})|\d{1,11}";
                string pattern = @"^[_0-9-]*$";
                return Regex.Match(TelNum, pattern).Success;
            }
        }

        public static bool IsValidTelNum4Check(string TelNum)
        {
            if (TelNum == "")
            {
                return true;
            }
            else
            {
                //string pattern = @"(((\(\d{2,3}\))|(\d{2,3}-))\d{1,8})|\d{1,11}";
                string pattern = @"^[0-9-\*]*$";
                return Regex.Match(TelNum, pattern).Success;
            }
        }



        /// <summary>
        /// 检验日期格式是否正确 YYYY-MM-DD(不允许为空)
        /// </summary>
        /// <param name="dateStr">日期字符串</param>
        /// <returns>true=合法, false=非法</returns>
        public static bool IsValidDateStr(string dateStr)
        {
            if ((dateStr == null) || (dateStr.Length != 10))
                return false;
            try
            {
                DateTime dt = DateTime.ParseExact(dateStr, "yyyy-MM-dd", null);
                return (dt > MinDateTime);
            }
            catch
            {
                return false;
            }
        }

        /// <summary> 检验是否是合格的正整数字符串
        /// 
        /// </summary>
        /// <param name="str">字符串
        /// </param>
        public static bool IsValidInt(string str)
        {
            if ((str == null) || (str.Length == 0))
                return false;
            try
            {
                int i = int.Parse(str);
                return i >= 0;
            }
            catch
            {
                return false;
            }
        }

        /// <summary> 检验是否是合格的数字、包括正数和负数
        /// 
        /// </summary>
        /// <param name="str">字符串
        /// </param>
        public static bool IsValidNumber(string str)
        {
            if ((str == null) || (str.Length == 0))
                return false;
            try
            {
                int i = int.Parse(str);
                return true;

            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 检验是否是合格的整数字符串
        /// </summary>
        /// <param name="str">待检验字符</param>
        /// <param name="isAllowEmpty">是否允许为空(true=是, false=否)</param>
        /// <returns>true=合法, false=非法</returns>
        public static bool IsValidInt(string str, bool isAllowEmpty)
        {
            if (string.IsNullOrEmpty(str) == true)
            {
                return isAllowEmpty;
            }

            try
            {
                int i = int.Parse(str);

                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 检验是否是合格的整数字符串(>=0)
        /// </summary>
        /// <param name="str">待检验字符</param>
        /// <param name="isAllowEmpty">是否允许为空(true=是, false=否)</param>
        /// <returns>true=合法, false=非法</returns>
        public static bool IsValidPlusInt(string str, bool isAllowEmpty)
        {
            if (string.IsNullOrEmpty(str) == true)
            {
                return isAllowEmpty;
            }

            try
            {
                int i = int.Parse(str);
                return i >= 0;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 检查输入字符串是否为指定范围内的整数，包含临界值
        /// </summary>
        /// <param name="s">输入字符串</param>
        /// <param name="min">最小值</param>
        /// <param name="max">最大值</param>
        /// <returns></returns>
        public static bool IsValidIntRange(string s, int min, int max)
        {
            if (string.IsNullOrEmpty(s)) return false;
            try
            {
                int i = int.Parse(s);
                return i >= min && i <= max;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 检验是否是合格的整数字符串(>0)
        /// </summary>
        /// <param name="str">待检验字符</param>
        /// <param name="isAllowEmpty">是否允许为空(true=是, false=否)</param>
        /// <returns>true=合法, false=非法</returns>
        public static bool IsValid4Int(string str, bool isAllowEmpty)
        {
            if (string.IsNullOrEmpty(str) == true)
            {
                return isAllowEmpty;
            }

            try
            {
                int i = int.Parse(str);

                if (i > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }


        /// <summary> 检验是否是合格的数字字符串
        /// 
        /// </summary>
        /// <param name="str">字符串
        /// </param>
        public static bool IsValidDouble(string str)
        {
            if ((str == null) || (str.Length == 0))
                return false;
            try
            {
                double.Parse(str);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary> 检验是否是合格的整数字符串并且为0～100
        /// 
        /// </summary>
        /// <param name="str">字符串
        /// </param>
        public static bool IsValidInt100(string str)
        {
            if ((str == null) || (str.Length == 0))
                return false;
            try
            {
                if ((int.Parse(str) <= 100) && (int.Parse(str) >= 0))
                    return true;
                else
                    return false;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 检验是否是合格的整数字符串( 允许负数)
        /// </summary>
        /// <param name="str">待检测的字符串</param>
        /// <param name="isAllowEmpty">是否允许为空(允许=true, 不允许=false)</param>
        /// <returns>true=合法, false=非法</returns>
        public static bool IsValidIntAllowNegative(string str, bool isAllowEmpty)
        {
            if (string.IsNullOrEmpty(str) == true)
            {
                return isAllowEmpty;
            }

            try
            {
                int i = int.Parse(str);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        ///判断时间格式是否正确 YYYY-MM-DD，允许时间为空（供查询时，选择查询条件时间为空的情况下使用）
        /// </summary>
        /// <param name="date">日期字符串</param>
        /// <returns></returns>
        public static bool IsValidDate(string dateStr)
        {
            if (dateStr == "")
            {
                return true;
            }
            else
            {
                try
                {
                    DateTime xx = DateTime.ParseExact(dateStr, "yyyy-MM-dd", null);
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        /// <summary>
        /// 判断时间格式是否正确 yyyy-MM，允许时间为空
        /// </summary>
        /// <param name="dateStr">日期字符串</param>
        /// <returns>true=合法, false=非法</returns>
        public static bool IsValidShortDate(string dateStr)
        {
            if (string.IsNullOrEmpty(dateStr) == true)
            {
                return true;
            }
            else
            {
                //验证: "yyyy-MM"长度为7
                if (dateStr.Trim().Length != 7)
                {
                    return false;
                }

                if (dateStr.IndexOf("-") != 4)
                {
                    return false;
                }

                try
                {
                    int year = int.Parse(dateStr.Substring(0, 4));
                    int month = int.Parse(dateStr.Substring(5, 2));

                    if (year < 1000 || year > 10000 || month < 1 || month > 12)
                    {
                        return false;
                    }

                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        /// <summary>
        /// 验证结束日期>=开始日期
        /// 日期格式为"yyyy-MM"
        /// </summary>
        /// <param name="startDT">开始日期</param>
        /// <param name="endDT">结束日期</param>
        /// <returns>true=合法, false=非法</returns>
        public static bool StartDTCompareToEndDT(string startDT, string endDT)
        {
            try
            {
                if ((startDT.Trim().Length != 7) || (endDT.Trim().Length != 7))
                {
                    return true;
                }

                int start = int.Parse(startDT.Substring(0, 4)) * 100 + int.Parse(startDT.Substring(5, 2));

                int end = int.Parse(endDT.Substring(0, 4)) * 100 + int.Parse(endDT.Substring(5, 2));

                if (start > end)
                {
                    return false;
                }

                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 验证日期不能大于现在日期
        /// 日期格式为"yyyy-MM"
        /// </summary>
        /// <param name="strDate">日期字符串</param>
        /// <returns>true=合法, false=非法</returns>
        public static bool CheckDateLessThanNow(string strDate)
        {
            //验证: "yyyy-MM"长度为7, 若日期不合法, 则返回True不作处理.
            if (string.IsNullOrEmpty(strDate) == true || strDate.Trim().Length != 7)
            {
                return true;
            }

            try
            {
                int date = int.Parse(strDate.Substring(0, 4)) * 100 + int.Parse(strDate.Substring(5, 2));

                int now = DateTime.Now.Year * 100 + DateTime.Now.Month;

                if (date > now)
                {
                    return false;
                }

                return true;
            }
            catch
            {
                return true;
            }
        }

        /// <summary>
        /// 验证URL地址合法性
        /// </summary>
        /// <param name="strURL">URL地址</param>
        /// <param name="isAllowNull">是否允许为空(True=是, False=否)</param>
        /// <returns>true=合法, false=非法</returns>
        public static bool IsValidURLAddress(string strURL, bool isAllowNull)
        {
            if (string.IsNullOrEmpty(strURL) == true)
            {
                return isAllowNull;
            }

            try
            {
                string pattern = @"[a-zA-z]+://[^\s]*";
                return Regex.Match(strURL, pattern).Success;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 验证日期是否为空日期
        /// </summary>
        public static bool IsNullDateTime(DateTime dt)
        {
            if (dt == null)
                return true;

            int year = dt.Year;

            if (year < 1900)
                return true;
            else
                return false;

        }

        /// <summary>
        /// 验证是否为合法的十进制数据(decimal)
        /// </summary>
        /// <param name="str">检验字符串</param>
        /// <param name="isAllowEmpty">是否允许为空(true=是, false=否)</param>
        /// <returns>true=合法, false=非法</returns>
        public static bool IsValidDecimal(string str, bool isAllowEmpty)
        {
            if (string.IsNullOrEmpty(str))
            {
                return isAllowEmpty;
            }

            try
            {
                decimal temp = decimal.Parse(str);

                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 验证输入字符串的长度是否在指定的范围内,临界值在范围内。
        /// </summary>
        /// <param name="s">待检验的字符串</param>
        /// <param name="min">最小长度</param>
        /// <param name="max">最大长度</param>
        /// <returns></returns>
        public static bool IsValidStringRange(string s, int min, int max)
        {
            if (min > max) return false;
            if (s == null)
                s = "";
            return (s.Length >= min) && (s.Length <= max);
        }
        /// <summary>
        /// 是否符合规范的用户名
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string IsLegalUserName(string username, int min, int max)
        {
            string error = string.Empty;
            if (username == "" || username == "输入注册用户名")
            {
                return "请输入注册用户名";
            }
            else if (Regex.IsMatch(username, @"^\d.*$"))
            {
                //用户名不能以数字开头
                return "用户名不能以数字开头";
            }
            else if (GetStringLength(username) < min)
            {
                //用户名长度小于6个字符
                return string.Format("用户名长度小于{0}个字符", min);
            }
            else if (!Regex.IsMatch(username, @"^\w+$"))
            {
                //用户名只能包含_,英文字母，数字
                return "用户名只能包含_,英文字母，数字";
            }
            else if (!Regex.IsMatch(username, @"^([a-z]|[A-Z])[0-9a-zA-Z_]+$"))
            {
                //用户名只能英文字母开头
                return "用户名只能英文字母开头";
            }
            else if (!Regex.IsMatch(username, @"[0-9a-zA-Z]+$"))
            {
                //用户名只能英文字母或数字结尾
                return "用户名只能英文字母或数字结尾";
            }
            else if (GetStringLength(username) > max)
            {
                //用户名长度大于18个字符
                return string.Format("用户名长度大于{0}个字符", max);
            }
            return "";
        }
        /// <summary>
        /// 混合验证输入字符串输入长度
        /// 包括中文
        /// </summary>
        /// <param name="str">要判断的字符串</param>
        /// <param name="i">长度</param>
        /// <returns></returns>
        public static bool IsValidStringRangeMix(string s, int min, int max)
        {
            int m = GetStringLength(s);
            return (m >= min) && (m <= max);
        }
        /// <summary>
        /// 返回字符串输入长度
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static int GetStringLength(string s)
        {
            byte[] b = Encoding.Default.GetBytes(s);
            return b.Length;
        }
        ///<summary>
        /// 验证结束日期>开始日期
        /// 日期格式为"yyyy-MM-dd"
        /// </summary>
        /// <param name="startDT">开始日期</param>
        /// <param name="endDT">结束日期</param>
        /// <returns>true=合法, false=非法</returns>
        public static bool StartDTCompareToEndDT4Date(string startDT, string endDT)
        {
            try
            {
                if (!IsValidDateStr(startDT) || !IsValidDateStr(endDT))
                {
                    return false;
                }
                else
                {
                    DateTime startDate = DateTime.Parse(startDT);
                    DateTime endDate = DateTime.Parse(endDT);
                    return startDate < endDate ? true : false;
                }
            }
            catch
            {
                return false;
            }
        }

        ///<summary>
        /// 验证结束日期>=开始日期
        /// 日期格式为"yyyy-MM-dd"
        /// </summary>
        /// <param name="startDT">开始日期</param>
        /// <param name="endDT">结束日期</param>
        /// <returns>true=合法, false=非法</returns>
        public static bool StartDTCompareAndEqualToEndDT4Date(string startDT, string endDT)
        {
            try
            {
                if (!IsValidDateStr(startDT) || !IsValidDateStr(endDT))
                {
                    return false;
                }
                else
                {
                    DateTime startDate = DateTime.Parse(startDT);
                    DateTime endDate = DateTime.Parse(endDT);
                    return startDate <= endDate ? true : false;
                }
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 验证日期不能大于等于现在日期
        /// 日期格式为"yyyy-MM-dd"
        /// </summary>
        /// <param name="strDate">日期字符串</param>
        /// <returns>true=合法, false=非法</returns>
        public static bool CheckDateLessThanNow4Date(string strDate)
        {
            try
            {
                if (!IsValidDateStr(strDate))
                {
                    return false;
                }
                else
                {
                    DateTime date = DateTime.Parse(strDate);
                    return date <= System.DateTime.Now ? true : false;
                }
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 验证日期不能大于现在日期
        /// 日期格式为"yyyy-MM-dd"
        /// </summary>
        /// <param name="strDate">日期字符串</param>
        /// <returns>true=合法, false=非法</returns>
        public static bool CheckDateLessAndNotEqualThanNow4Date(string strDate)
        {
            try
            {
                if (!IsValidDateStr(strDate))
                {
                    return false;
                }
                else
                {
                    DateTime date = DateTime.Parse(strDate).AddDays(1);
                    return date < System.DateTime.Now ? true : false;
                }
            }
            catch
            {
                return false;
            }
        }


        /// <summary>
        /// 替换js编辑框中的空格
        /// </summary>
        /// <param name="strContent">需要替换的内容</param>
        /// <returns>替换后的内容</returns>
        public static string EditorReplace(string strContent)
        {
            string Result = null;
            try
            {
                Result = Regex.Replace(strContent, @"<( )*p([^>])*>", "", RegexOptions.IgnoreCase);
                Result = Regex.Replace(Result, @"&nbsp;", "", RegexOptions.IgnoreCase);
                Result = Regex.Replace(Result, @"</p>", "", RegexOptions.IgnoreCase);
                Result = Regex.Replace(Result, @"\r\n", "", RegexOptions.IgnoreCase);
                return Result.Trim();
            }
            catch
            {
                return Result;
            }
        }

        /// <summary>
        /// 检验是否是合格的整数字符串(大于0并且第一个数字不能为0)
        /// </summary>
        /// <param name="str">待检验字符</param>
        /// <param name="isAllowEmpty">是否允许为空(true=是, false=否)</param>
        /// <returns>true=合法, false=非法</returns>
        public static bool IsValid4Int0(string str, bool isAllowEmpty)
        {
            if (string.IsNullOrEmpty(str) == true)
            {
                return isAllowEmpty;
            }

            try
            {
                //判断第一个数字是否是0(如：099为不合格的字符串)
                int j = int.Parse(str.Substring(0, 1));
                if (j == 0)
                {
                    return false;
                }
                else
                {
                    int i = int.Parse(str);
                    if (i > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }

            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 验证是否为小数(保留一位小数)
        /// </summary>
        /// <param name="str">待检测字符</param>
        /// <returns>true=合法, false=非法</returns>
        public static bool IsValidOneDecimal(string str)
        {
            return Regex.IsMatch(str, "^[0-9]+(.[0-9]{1})?$");
        }

        /// <summary>
        /// 验证是否为小数(保留二位小数)
        /// </summary>
        /// <param name="str">待检测字符</param>
        /// <returns>true=合法, false=非法</returns>
        public static bool IsValidTwoDecimal(string str)
        {
            return Regex.IsMatch(str, "^[0-9]+(.[0-9]{2})?$");
        }

        /// <summary>
        /// 比较两个日期时间的差值
        /// </summary>
        /// <param name="dtStart">开始时间</param>
        /// <param name="dtEnd">结束时间</param>
        /// <returns>时间差值(TimeSpan)</returns>
        public static TimeSpan CompareDateTime(string dtStart, string dtEnd)
        {
            TimeSpan ts = new TimeSpan();
            try
            {
                ts = DateTime.Parse(dtEnd) - DateTime.Parse(dtStart);
            }
            catch
            {

            }

            return ts;
        }

        /// <summary>
        /// 替换传递过来的内容的Form标签为空
        /// </summary>
        /// <param name="strContent">需替换Form标签的内容</param>
        /// <returns>把Form标签替换为空后的内容</returns>
        public static string ReplaceFormSpan(string strContent)
        {
            if (!string.IsNullOrEmpty(strContent))
            {
                strContent = Regex.Replace(strContent, @"<( )*form([^>])*>", "", RegexOptions.IgnoreCase);
                strContent = Regex.Replace(strContent, @"<( )*/form([^>])*>", "", RegexOptions.IgnoreCase);
            }
            return strContent;
        }

        /// <summary>
        /// 检验"邮编"合法性
        /// </summary>
        /// <param name="str">待判断字符</param>
        /// <param name="isAllowEmpty">是否允许为空(true=是, false=否)</param>
        /// <returns>true=合法, false=非法</returns>
        public static bool IsValidPostcode(string str, bool isAllowEmpty)
        {
            if (string.IsNullOrEmpty(str) == true)
            {
                return isAllowEmpty;
            }
            else
            {
                string pattern = @"\d{6}";
                return Regex.Match(str, pattern).Success;
            }
        }
        #region 检查输入的参数是不是某些定义好的特殊字符：这个方法目前用于密码输入的安全检查
        /// <summary>
        /// 检查输入的参数是不是某些定义好的特殊字符：这个方法目前用于密码输入的安全检查
        /// </summary>
        public static bool IsContainSpecChar(string strInput)
        {
            string[] list = new string[] { "123456", "654321" };
            bool result = new bool();
            for (int i = 0; i < list.Length; i++)
            {
                if (strInput == list[i])
                {
                    result = true;
                    break;
                }
            }
            return result;
        }
        #endregion

        /// <summary>
        /// 判断对象是否为Int32类型的数字
        /// </summary>
        /// <param name="Expression"></param>
        /// <returns></returns>
        public static bool IsNumeric(object expression)
        {
            if (expression != null)
                return IsNumeric(expression.ToString());

            return false;

        }

        /// <summary>
        /// 判断对象是否为Int32类型的数字
        /// </summary>
        /// <param name="Expression"></param>
        /// <returns></returns>
        public static bool IsNumeric(string expression)
        {
            if (expression != null)
            {
                string str = expression;
                if (str.Length > 0 && str.Length <= 11 && Regex.IsMatch(str, @"^[-]?[0-9]*[.]?[0-9]*$"))
                {
                    if ((str.Length < 10) || (str.Length == 10 && str[0] == '1') || (str.Length == 11 && str[0] == '-' && str[1] == '1'))
                        return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 是否为Double类型
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static bool IsDouble(object expression)
        {
            if (expression != null)
                return Regex.IsMatch(expression.ToString(), @"^([0-9])[0-9]*(\.\w*)?$");

            return false;
        }

        /// <summary>
        /// 判断给定的字符串数组(strNumber)中的数据是不是都为数值型
        /// </summary>
        /// <param name="strNumber">要确认的字符串数组</param>
        /// <returns>是则返加true 不是则返回 false</returns>
        public static bool IsNumericArray(string[] strNumber)
        {
            if (strNumber == null)
                return false;

            if (strNumber.Length < 1)
                return false;

            foreach (string id in strNumber)
            {
                if (!IsNumeric(id))
                    return false;
            }
            return true;
        }
    }
}
